import 'package:flutter/material.dart';
import 'package:intrinsic_grid_view/intrinsic_grid_view.dart';
import '../../common/api/live_audit_api.dart';
import '../../common/api/site_api.dart';
import '../../common/const/app_color_const.dart';
import '../../common/enum/btn_enum.dart';
import '../../common/util/common_util.dart';
import '../../widget/card_container.dart';
import '../../widget/empty.dart';
import '../../widget/error_img.dart';
import '../../widget/form_item.dart';
import '../../widget/icon_outlined_btn.dart';
import '../../widget/img_grid_list.dart';
import '../../widget/ripple_card_container.dart';
import 'live_audit_dialog.dart';

class LiveAuditDetailPage extends StatefulWidget {
  final Map arguments;

  const LiveAuditDetailPage({Key? key, required this.arguments})
      : super(key: key);

  @override
  State<LiveAuditDetailPage> createState() => LiveAuditDetailPageState();
}

class LiveAuditDetailPageState extends State<LiveAuditDetailPage> {
  Map? data;
  List<String> tiktokScreenshotDataList = [];
  List<String> applyPicList = [];

  @override
  void initState() {
    super.initState();
    initData();
  }

  @override
  void dispose() {
    super.dispose();
  }

  initData() {
    LiveAuditApi.getLiveAudit({"id": widget.arguments['id']}).then((res) {
      if (null != res.data && res.data["isSuccess"]) {
        data = res.data["data"];

        if (null != res.data["data"] &&
            null != res.data["data"]["applyPicList"]) {
          for (var img in res.data["data"]["applyPicList"]) {
            if (CommonUtil.startWithHttp(img)) {
              applyPicList.add(img);
            } else {
              applyPicList.add(CommonUtil.getEnvConfig()["oosUrl"] + img);
            }
          }
        }

        if (null != res.data["data"] &&
            null != res.data["data"]["tiktokScreenshotList"]) {
          for (var img in res.data["data"]["tiktokScreenshotList"]) {
            if (CommonUtil.startWithHttp(img)) {
              tiktokScreenshotDataList.add(img);
            } else {
              tiktokScreenshotDataList
                  .add(CommonUtil.getEnvConfig()["oosUrl"] + img);
            }
          }
        }
        setState(() {});
      } else {
        CommonUtil.showToast(res.data["msg"]);
      }
    });
  }

  submitLiveAudit(data) {
    LiveAuditApi.submitLiveAudit(data).then((res) {
      if (null != res.data && res.data["isSuccess"]) {
        CommonUtil.showToast("审核完成！");
        Navigator.of(context).pushNamed("/liveAudit");
      } else {
        CommonUtil.showToast(res.data["msg"]);
      }
    });
  }

  reject() {
    LiveAuditDialog.showLiveAuditRejectDialog((String content, cb) {
      if (content.trim().isEmpty) {
        CommonUtil.showToast("请输入拒绝理由！");
      } else {
        submitLiveAudit({
          "applyFormId": widget.arguments['id'],
          "reviewRemark": content,
          "reviewStatus": 9,
        });
        cb();
      }
    });
  }

  agree() {
    CommonUtil.showSingleConfirmDialog(
        "liveAuditDetailAgreeTag", 135, "您确认通过吗？", (cb) {
      submitLiveAudit({
        "applyFormId": widget.arguments['id'],
        "reviewRemark": "审核通过",
        "reviewStatus": 2,
      });
      cb();
    });
  }

  liveAuditDetailBaseInfoBodyBuilder(context) {
    if (null != data) {
      return Container(
        color: lightGrayColor8,
        padding: const EdgeInsets.all(8.0),
        child: Column(children: [
          LayoutBuilder(builder: (buildContext, constraints) {
            return FormItem(
              label: '联系人',
              valueWidth: constraints.maxWidth - 60,
              value: CommonUtil.valueEmptyToDefStr(data!['contactUserName']),
            );
          }),
          FormItem(
            label: '手机号',
            value: CommonUtil.valueEmptyToDefStr(data!['contactTel']),
          ),
          LayoutBuilder(builder: (buildContext, constraints) {
            return FormItem(
              label: '渠道',
              valueWidth: constraints.maxWidth - 50,
              value: CommonUtil.valueEmptyToDefStr(data!['rtoChannelName']),
            );
          }),
          LayoutBuilder(builder: (buildContext, constraints) {
            return FormItem(
              label: '店铺类型',
              valueWidth: constraints.maxWidth - 70,
              value: CommonUtil.valueEmptyToDefStr(data!['rtoShopTypeName']),
            );
          }),
        ]),
      );
    } else {
      return const Padding(
        padding: EdgeInsets.only(top: 10.0),
        child: Empty(),
      );
    }
  }

  liveAuditDetailTiktokInfoBodyBuilder(context) {
    if (null != data) {
      return Container(
        color: lightGrayColor8,
        padding: const EdgeInsets.all(8.0),
        child: Column(children: [
          LayoutBuilder(builder: (buildContext, constraints) {
            return FormItem(
              label: '用户名',
              valueWidth: constraints.maxWidth - 60,
              value: CommonUtil.valueEmptyToDefStr(data!['tiktokUserName']),
            );
          }),
          LayoutBuilder(builder: (buildContext, constraints) {
            return FormItem(
              label: '抖音号',
              valueWidth: constraints.maxWidth - 60,
              value: CommonUtil.valueEmptyToDefStr(data!['tiktokNum']),
            );
          }),
          LayoutBuilder(builder: (buildContext, constraints) {
            return FormItem(
              label: 'UID',
              valueWidth: constraints.maxWidth - 40,
              value: CommonUtil.valueEmptyToDefStr(data!['tiktokUid']),
            );
          }),
          LayoutBuilder(builder: (buildContext, constraints) {
            return FormItem(
              label: '获赞数',
              valueWidth: constraints.maxWidth - 60,
              value: CommonUtil.valueEmptyToDefStr(data!['tiktokLikeNum']),
            );
          }),
          LayoutBuilder(builder: (buildContext, constraints) {
            return FormItem(
              label: '粉丝数',
              valueWidth: constraints.maxWidth - 60,
              value: CommonUtil.valueEmptyToDefStr(data!['tiktokFansNum']),
            );
          }),
          LayoutBuilder(builder: (buildContext, constraints) {
            return FormItem(
              label: '是否有直播经验',
              valueWidth: constraints.maxWidth - 115,
              value: (data!['hasTiktokLiveExp'] ?? '') != '' ? '是' : '否',
            );
          }),
          tiktokScreenshotDataList.isNotEmpty
              ? ImgGridList(
                  imgList: tiktokScreenshotDataList,
                )
              : Container(),
        ]),
      );
    } else {
      return const Padding(
        padding: EdgeInsets.only(top: 10.0),
        child: Empty(),
      );
    }
  }

  liveAuditDetailMoreInfoBodyBuilder(context) {
    if (null != data) {
      return Container(
        color: lightGrayColor8,
        padding: const EdgeInsets.all(8.0),
        child: Column(children: [
          LayoutBuilder(builder: (buildContext, constraints) {
            return FormItem(
              label: '补充说明',
              valueWidth: constraints.maxWidth - 70,
              value: CommonUtil.valueEmptyToDefStr(data!['applyResone']),
            );
          }),
          FormItem(label: '补充图片', value: applyPicList.isNotEmpty ? '' : '-'),
          applyPicList.isNotEmpty
              ? ImgGridList(
                  imgList: applyPicList,
                )
              : Container(),
        ]),
      );
    } else {
      return const Padding(
        padding: EdgeInsets.only(top: 10.0),
        child: Empty(),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("主播审核详情"),
      ),
      body: SingleChildScrollView(
          controller: ScrollController(),
          child: Padding(
              padding: const EdgeInsets.all(10.0),
              child: CardContainer(
                  bodyPadding: const EdgeInsets.all(0.0),
                  bodyBuilder: (context) => Column(
                        children: [
                          CardContainer(
                              title: '基础信息',
                              decorationVisible: false,
                              bodyBuilder: (context) =>
                                  liveAuditDetailBaseInfoBodyBuilder(context)),
                          CardContainer(
                              title: '抖音账号信息',
                              decorationVisible: false,
                              bodyBuilder: (context) =>
                                  liveAuditDetailTiktokInfoBodyBuilder(
                                      context)),
                          CardContainer(
                              title: '更多信息',
                              decorationVisible: false,
                              bodyBuilder: (context) =>
                                  liveAuditDetailMoreInfoBodyBuilder(context)),
                          Container(
                              padding: const EdgeInsets.only(
                                  left: 12, right: 12, bottom: 10),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Expanded(
                                    child: Padding(
                                      padding: const EdgeInsets.only(right: 5),
                                      child: IconOutlinedBtn(
                                          text: '同意',
                                          btnType: BtnType.green,
                                          onPressed: () {
                                            agree();
                                          }),
                                    ),
                                  ),
                                  Expanded(
                                      child: Padding(
                                    padding: const EdgeInsets.only(left: 5),
                                    child: IconOutlinedBtn(
                                        text: '拒绝',
                                        btnType: BtnType.red,
                                        onPressed: () {
                                          reject();
                                        }),
                                  ))
                                ],
                              ))
                        ],
                      )))),
    );
  }
}
